<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <div>
        <span>姓名:</span>
        <input type="text" name="xm" v-model="xm" />
      </div>
      <div>
        <span>年龄:</span>
        <input
          type="number"
          name="nl"
          v-model="nl
        "
        />
      </div>
      <div>
        <span>性别:</span>
        <select v-model="xb">
          <option value="男">男</option>
          <option value="女">女</option>
        </select>
      </div>
      <div>
        <button @click="fn">添加/修改</button>
      </div>
      <div>
        <table border="1" cellpadding="10" cellspacing="0">
          <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item,index) in arr" :key="index">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.age}}</td>
            <td>{{item.xbiie}}</td>
            <td>
              <button @click="sc(index)">删除</button>
              <button @click="fu(item)">编辑</button>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <script src="./vue (2).js"></script>
    <script>
      const app = new Vue({
        el: '#app',
        data: {
          arr: [{ id: 1, name: '翘嘴', age: '23', xbiie: '男' }],
          xm: '',
          nl: '',
          xb: '',
          att: false,
          id: ''
        },

        methods: {
          fn() {
            if (this.att === false) {
              if (this.xm === '' && this.nl === '') {
                return alert('请输入姓名年龄')
              }

              this.arr.push({
                id: this.arr.length + 1,
                name: this.xm,
                age: this.nl,
                xbiie: this.xb
              })
            }

            if (this.att) {
              this.att = this.arr.map(item => {
                if (this.id === item.id) {
                  item.name = this.xm
                  item.age = this.nl
                  item.xbiie = this.xb
                }
              })
            }
            this.xm = ''
            this.nl = ''
            this.xb = ''
            this.att = false
          },
          fu(item) {
            this.att = true
            this.xm = item.name
            this.nl = item.age
            this.xb = item.xbiie
            this.id = item.id
          },
          sc(idx) {
            this.arr = this.arr.filter((item, i) => i !== idx)
          }
        }
      })
    </script>
  </body>
</html>
